package com.njupt.dtu;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

public class strTest {
	public static String readFileContent(String fileName) throws FileNotFoundException {
    	FileInputStream file = new FileInputStream(fileName);
        BufferedReader reader = null;
        StringBuffer sbf = new StringBuffer();
        try {
            reader = new BufferedReader(new InputStreamReader(file));
            String tempStr;
            while ((tempStr = reader.readLine()) != null) {
                sbf.append(tempStr);
                sbf.append("\n");
            }
            reader.close();
            return sbf.toString();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
        return sbf.toString();
    }
	
	public static void tican_shebei(String tmpStr,SerialPortUtils serialPort) {
		String f = "";
    	String k ="";
    	String[] str = tmpStr.split("\n");
    	for(int i =0;i<str.length;i++) {
    		int a = str[i].indexOf("=");
    		if(a!=-1) {
        		f=str[i].substring(0,a);
        		switch(f) {
        		case "name_dian": 
        			k = str[i].substring(a+1,str[i].length());
        			serialPort.name_dian=k;
        			break;
        		case "location_dian":
        			k = str[i].substring(a+1,str[i].length());
        			serialPort.location_dian=k;
        			break;
        		case "name_wang":
        			k = str[i].substring(a+1,str[i].length());
        			serialPort.name_wang=k;
        			break;
        		case "rx":
        			k = str[i].substring(a+1,str[i].length());
        			serialPort.rx=k;
        			break;
        		case "tx":
        			k = str[i].substring(a+1,str[i].length());
        			serialPort.tx=k;
        			break;
        		case "time":
        			k = str[i].substring(a+1,str[i].length());
        			serialPort.time=k;
        			break;
        		default:
        			break;
        		}
        		System.out.println(k);
    		}
    	}
	}
	
	public static void tican_device(String tmpStr,SerialPortUtils serialPort) {
		String f = "";
    	String k ="";
    	String[] str = tmpStr.split("\n");
    	for(int i =0;i<str.length;i++) {
    		int a = str[i].indexOf("=");
    		if(a!=-1) {
        		f=str[i].substring(0,a);
        		switch(f) {
        		case "name_device": 
        			k = str[i].substring(a+1,str[i].length());
        			serialPort.name_device=k;
        			break;
        		case "type_device":
        			k = str[i].substring(a+1,str[i].length());
        			serialPort.type_device=k;
        			break;
        		case "location_device":
        			k = str[i].substring(a+1,str[i].length());
        			serialPort.location_device=k;
        			break;
        		case "version_device":
        			k = str[i].substring(a+1,str[i].length());
        			serialPort.version_device=k;
        			break;
        		default:
        			break;
        		}
        		System.out.println(k);
    		}
    	}
	}
	
	public static void tican_net(String tmpStr,SerialPortUtils serialPort) {
		String f = "";
    	String k ="";
    	String[] str = tmpStr.split("\n");
    	for(int i =0;i<str.length;i++) {
    		int a = str[i].indexOf("=");
    		if(a!=-1) {
        		f=str[i].substring(0,a);
        		switch(f) {
        		case "devip0": 
        			k = str[i].substring(a+1,str[i].length());
        			serialPort.devip0[0]=strSplit(k)[0];
        			serialPort.devip0[1]=strSplit(k)[1];
        			serialPort.devip0[2]=strSplit(k)[2];
        			serialPort.devip0[3]=strSplit(k)[3];
        			break;
        		case "netmask0":
        			k = str[i].substring(a+1,str[i].length());
        			serialPort.netmask0[0]=strSplit(k)[0];
        			serialPort.netmask0[1]=strSplit(k)[1];
        			serialPort.netmask0[2]=strSplit(k)[2];
        			serialPort.netmask0[3]=strSplit(k)[3];
        			break;
        		case "defaultgw0":
        			k = str[i].substring(a+1,str[i].length());
        			serialPort.defaultgw0[0]=strSplit(k)[0];
        			serialPort.defaultgw0[1]=strSplit(k)[1];
        			serialPort.defaultgw0[2]=strSplit(k)[2];
        			serialPort.defaultgw0[3]=strSplit(k)[3];
        			break;
        		case "devip1":
        			k = str[i].substring(a+1,str[i].length());
        			serialPort.devip1[0]=strSplit(k)[0];
        			serialPort.devip1[1]=strSplit(k)[1];
        			serialPort.devip1[2]=strSplit(k)[2];
        			serialPort.devip1[3]=strSplit(k)[3];
        			break;
        		case "netmask1":
        			k = str[i].substring(a+1,str[i].length());
        			serialPort.netmask1[0]=strSplit(k)[0];
        			serialPort.netmask1[1]=strSplit(k)[1];
        			serialPort.netmask1[2]=strSplit(k)[2];
        			serialPort.netmask1[3]=strSplit(k)[3];
        			break;
        		case "defaultgw1":
        			k = str[i].substring(a+1,str[i].length());
        			serialPort.defaultgw1[0]=strSplit(k)[0];
        			serialPort.defaultgw1[1]=strSplit(k)[1];
        			serialPort.defaultgw1[2]=strSplit(k)[2];
        			serialPort.defaultgw1[3]=strSplit(k)[3];
        			break;
        		default:
        			break;
        		}
        		System.out.println(k);
    		}
    	}
	}
	
	public static void tican_dial(String tmpStr,SerialPortUtils serialPort) {
		String f = "";
    	String k ="";
    	String[] str = tmpStr.split("\n");
    	for(int i =0;i<str.length;i++) {
    		int a = str[i].indexOf("=");
    		if(a!=-1) {
        		f=str[i].substring(0,a);
        		switch(f) {
        		case "apn0": 
        			k = str[i].substring(a+1,str[i].length());
        			serialPort.apn0=k;
        			break;
        		case "code0":
        			k = str[i].substring(a+1,str[i].length());
        			serialPort.code0=k;
        			break;
        		case "name0":
        			k = str[i].substring(a+1,str[i].length());
        			serialPort.name0=k;
        			break;
        		case "pwd0":
        			k = str[i].substring(a+1,str[i].length());
        			serialPort.pwd0=k;
        			break;
        		case "pin0": 
        			k = str[i].substring(a+1,str[i].length());
        			serialPort.pin0=k;
        			break;
        		case "apn1":
        			k = str[i].substring(a+1,str[i].length());
        			serialPort.apn1=k;
        			break;
        		case "code1":
        			k = str[i].substring(a+1,str[i].length());
        			serialPort.code1=k;
        			break;
        		case "name1":
        			k = str[i].substring(a+1,str[i].length());
        			serialPort.name1=k;
        			break;
        		case "pwd1":
        			k = str[i].substring(a+1,str[i].length());
        			serialPort.pwd1=k;
        			break;
        		case "pin1":
        			k = str[i].substring(a+1,str[i].length());
        			serialPort.pin1=k;
        			break;
        		default:
        			break;
        		}
        		System.out.println(k);
    		}
    	}
	}
	
	public static void tican_policy(String tmpStr,SerialPortUtils serialPort) {
		String f = "";
    	String k ="";
    	String[] str = tmpStr.split("\n");
    	for(int i =0;i<str.length;i++) {
    		int a = str[i].indexOf("=");
    		if(a!=-1) {
        		f=str[i].substring(0,a);
        		switch(f) {
        		case "isenc1": 
        			k = str[i].substring(a+1,str[i].length());
        			serialPort.isenc1=k;
        			break;
        		case "protect_dtuip1":
        			k = str[i].substring(a+1,str[i].length());
        			serialPort.protect_dtuip1[0]=strSplit(k)[0];
        			serialPort.protect_dtuip1[1]=strSplit(k)[1];
        			serialPort.protect_dtuip1[2]=strSplit(k)[2];
        			serialPort.protect_dtuip1[3]=strSplit(k)[3];
        			break;
        		case "mainip1":
        			k = str[i].substring(a+1,str[i].length());
        			serialPort.mainip1[0]=strSplit(k)[0];
        			serialPort.mainip1[1]=strSplit(k)[1];
        			serialPort.mainip1[2]=strSplit(k)[2];
        			serialPort.mainip1[3]=strSplit(k)[3];
        			break;
        		case "protect_mainip1":
        			k = str[i].substring(a+1,str[i].length());
        			serialPort.protect_mainip1[0]=strSplit(k)[0];
        			serialPort.protect_mainip1[1]=strSplit(k)[1];
        			serialPort.protect_mainip1[2]=strSplit(k)[2];
        			serialPort.protect_mainip1[3]=strSplit(k)[3];
        			break;
        		default:
        			break;
        		}
        		System.out.println(k);
    		}
    	}
	}
	
	public static void tican_serial(String tmpStr,SerialPortUtils serialPort) {
		String f = "";
    	String k ="";
    	String[] str = tmpStr.split("\n");
    	for(int i =0;i<str.length;i++) {
    		int a = str[i].indexOf("=");
    		if(a!=-1) {
        		f=str[i].substring(0,a);
        		switch(f) {
        		case "rate": 
        			k = str[i].substring(a+1,str[i].length());
        			serialPort.rate=k;
        			break;
        		case "databit":
        			k = str[i].substring(a+1,str[i].length());
        			serialPort.databit=k;
        			break;
        		case "verifybit":
        			k = str[i].substring(a+1,str[i].length());
        			serialPort.verifybit=k;
        			break;
        		case "stopbit":
        			k = str[i].substring(a+1,str[i].length());
        			serialPort.stopbit=k;
        			break;
        		case "conbit":
        			k = str[i].substring(a+1,str[i].length());
        			serialPort.conbit=k;
        			break;
        		default:
        			break;
        		}
        		System.out.println(k);
    		}
    	}
	}
	
	public static void tican_101tcp(String tmpStr,SerialPortUtils serialPort) {
		String f = "";
    	String k ="";
    	String[] str = tmpStr.split("\n");
    	for(int i =0;i<str.length;i++) {
    		int a = str[i].indexOf("=");
    		if(a!=-1) {
        		f=str[i].substring(0,a);
        		switch(f) {
        		case "servers": 
        			k = str[i].substring(a+1,str[i].length());
        			serialPort.servers=k;
        			break;
        		case "localserver":
        			k = str[i].substring(a+1,str[i].length());
        			serialPort.localserver=k;
        			break;
        		default:
        			break;
        		}
        		System.out.println(k);
    		}
    	}
	}
	
	public static String[] strSplit(String k) {
		String[] kk=k.split("\\.");
		return kk; 
	}
	
}

